home *** CD-ROM | disk | FTP | other *** search
- Path: nntphub.cb.att.com!news
- From: arvind@cbgbcs.att.com (Arvind Sharma)
- Newsgroups: comp.lang.c
- Subject: Re: Is there subroutine in C?
- Date: Thu, 18 Jan 1996 14:24:59 GMT
- Organization: AT&T Bell Laboratories, Columbus, Ohio
- Message-ID: <4dmkrd$l35@nntpa.cb.att.com>
- References: <4an13a$d94@news.ust.hk>
- NNTP-Posting-Host: arvind586.cb.att.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- hclau@uxmail.ust.hk (S. Vegata) wrote:
-
-
- >Hello All,
-
- >I 'd like to ask if there is subroutine in C and if subroutine
- >can change the passed arguments. For example:
-
- >main()
- >{
- >int x,y;
- >x = 5; y = 10;
- >add_one(x,y);
-
- Use this instead
-
- add_one(&x, &y); /* Basically pass the address of the variables where the
- "value" is stored */
-
- >printf("x: %d y: %d\n",x,y);
- >}
-
- >add_one(x,y) /* subroutine ?? */
- Declare this way
- int *x, *y; /* integer pointers */
- >int x,y;
- >{
- >x = x + 1; y = y + 1;
- Change to
- *x = *x +1;
- *y = *y +1;
- >}
-
- >After running this program, x is still 5 and y is still 10.
- >I am not familiar with C and the "passing by value" in C
- >really troubles me.
-
- >Could anyoone kindly tell me how I should write the correct
- >subroutine and argument passing such that x and y will become
- >6 and 11 respectively after the subrountine add_one(x,y)?
-
- >Thank you very much
-
- >best regards,
-
- >Eric Lau.
-
- Arvind Sharma
- arvind@cbgbcs.att.com
- Columbus, OH, USA
-
-